﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.Rules.CApAppend
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;


namespace DnProcessSimulateCommands.RenameTool.Rules;

[Export(typeof (RuleBase))]
public class CApAppend : RuleBase
{
  private object[] args = new object[3];
  private string displayString = "";

  public CApAppend() => this.ArgsLength = 3;

  public override RuleBase CreateInstance() => (RuleBase) new CApAppend();

  public override int Position => 1;

  public override RuleWindowBase CreateWindow(Window parent)
  {
    return (RuleWindowBase) new CUiAppend(parent);
  }

  public override RuleMessage Execute(List<string> objects)
  {
    if (objects == null)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_NULL_REF_OBJ"));
    if (this.Args == null)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_NULL_REF_ARGS"));
    if (this.Args.Length != 3)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_ARG_COUNT"));
    if (!(this.Args[0] is bool) || !(this.Args[1] is int) || !(this.Args[2] is string))
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_ARG_TYPE"));
    bool isPrefix = (bool) this.Args[0];
    string str = (string) this.Args[2];
    if ((int) this.Args[1] == 0)
      return this.Append(objects, isPrefix, str);
    return (int) this.Args[1] == 1 ? this.Trim(objects, isPrefix, str) : this.TrimDigits(objects, isPrefix);
  }

  private RuleMessage Append(List<string> objects, bool isPrefix, string appendString)
  {
    PartialStringParser partialStringParser = new PartialStringParser();
    RuleMessage ruleMessage = partialStringParser.Parse(appendString);
    if (ruleMessage.Success)
    {
      List<Dictionary<string, string>> dictionaryList = new List<Dictionary<string, string>>();
      List<string> attributes = partialStringParser.GetAttributes();
      if (attributes != null && attributes.Count > 0)
      {
        List<Dictionary<string, string>> attributesValuesAsString = CApRenameToolManager.GetInstance().GetAttributesValuesAsString(attributes, out this.ruleState);
        partialStringParser.SetAttributesValues(attributesValuesAsString);
      }
      else
        this.ruleState = RuleState.Success;
      for (int index = 0; index < objects.Count; ++index)
        objects[index] = !isPrefix ? objects[index] + partialStringParser.GetNextString(index) : partialStringParser.GetNextString(index) + objects[index];
    }
    return ruleMessage;
  }

  private RuleMessage Trim(List<string> objects, bool isPrefix, string trimString)
  {
    uint result;
    if (!uint.TryParse(trimString.Trim(), out result))
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_APPEND_OR_TRIM_WRONG_TRIM"));
    if (isPrefix)
    {
      for (int index = 0; index < objects.Count; ++index)
        objects[index] = objects[index].Length >= (int) result ? objects[index].Remove(0, (int) result) : "";
    }
    else
    {
      for (int index = 0; index < objects.Count; ++index)
        objects[index] = objects[index].Length >= (int) result ? objects[index].Remove(objects[index].Length - (int) result, (int) result) : "";
    }
    return new RuleMessage(true);
  }

  private RuleMessage TrimDigits(List<string> objects, bool isPrefix)
  {
    if (isPrefix)
    {
      for (int index = 0; index < objects.Count; ++index)
      {
        bool flag = false;
        while (objects[index].Length > 0 && !flag)
        {
          if (!char.IsDigit(objects[index], 0))
            flag = true;
          else
            objects[index] = objects[index].Remove(0, 1);
        }
      }
    }
    else
    {
      for (int index = 0; index < objects.Count; ++index)
      {
        bool flag = false;
        while (objects[index].Length > 0 && !flag)
        {
          int num = objects[index].Length - 1;
          if (!char.IsDigit(objects[index], num))
            flag = true;
          else
            objects[index] = objects[index].Remove(num, 1);
        }
      }
    }
    return new RuleMessage(true);
  }

  public override object[] Args
  {
    get => this.args;
    set
    {
      if (value != null && value.Length == 3 && value[1] is bool)
      {
        this.args[0] = value[0];
        if ((bool) value[0])
          this.args[1] = (object) 0;
        else if (!(bool) value[0])
          this.args[1] = (object) 1;
        this.args[2] = value[2];
      }
      else
        this.args = value;
      this.UpdateDisplayString();
    }
  }

  public override string DisplayString => this.displayString;

  public override string Header => XamlResourceManager.GetString("RENAME_TOOL_APPEND_OR_TRIM");

  private void UpdateDisplayString()
  {
    try
    {
      this.displayString = "";
      if ((int) this.args[1] == 0)
      {
        bool? nullable = (bool?) this.args[0];
        bool flag = true;
        if (nullable.GetValueOrDefault() == flag & nullable.HasValue)
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_AT_APPEND_PREFIX"), this.args[2]);
        else
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_AT_APPEND_SUFFIX"), this.args[2]);
      }
      else if ((int) this.args[1] == 1)
      {
        bool? nullable = (bool?) this.args[0];
        bool flag = true;
        if (nullable.GetValueOrDefault() == flag & nullable.HasValue)
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_AT_TRIM_PREFIX"), this.args[2]);
        else
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_AT_TRIM_SUFFIX"), this.args[2]);
      }
      else
      {
        bool? nullable = (bool?) this.args[0];
        bool flag = true;
        if (nullable.GetValueOrDefault() == flag & nullable.HasValue)
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_AT_TRIM_PREFIX_DIGITS"));
        else
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_AT_TRIM_SUFFIX_DIGITS"));
      }
    }
    catch (Exception ex)
    {
    }
  }
}
